package diamondRingDao.impl;

import diamondRingDao.OrderDao;
import diamondRingEntity.Address;
import diamondRingEntity.DiamondRingEntity;
import diamondRingEntity.Order;
import diamondRingEntity.OrderGoods;
import tool.BaseDao;

import javax.servlet.jsp.jstl.sql.Result;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {

    @Override
    public int orderInsert(Order order) {
        int count=0;
        try {
            String sql="INSERT `order`(addressid,userid,orderNumberid,purchaseTime,transactionPrice,shoppingNum,remarks)\n" +
                    "VALUES(?,?,?,?,?,?,?)";
            Object[] param={order.getAddresId(), order.getUserID(),order.getOrderNumber(), order.getPurchaseTime(), order.getTransactionPrice(), order.getShoppingNum(), order.getRemarks()};
            count=this.executeUpdate(sql,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int orderGoodsInsert(OrderGoods orderGoods) {
        int count=0;
        try {
            String sql="INSERT ordergoods(goodsName,goodsId,orderId,texture,size,lettering)\n" +
                "VALUES(?,?,?,?,?,?)";
            Object[] param={orderGoods.getGoodsName(),orderGoods.getGoodsId(),orderGoods.getOrderid(),orderGoods.getTexture(),orderGoods.getSize(),orderGoods.getLettering()};
            count=this.executeUpdate(sql,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int orderFindId(String orderNumber) {
        int count=0;
        ResultSet rs=null;
        try {
            String sql="SELECT `order`.orderid\n" +
                    "FROM `order`\n" +
                    "WHERE `order`.orderNumberid=?";
            Object[] param={orderNumber};
            rs=this.executeQuery(sql,param);
            while (rs.next()){
                count=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int orderState(int goodsId, int userid) {
        ResultSet rs=null;
        int count=0;
        try {
            String sql="SELECT `order`.state\n" +
                    "FROM `order`\n" +
                    "INNER JOIN ordergoods ON ordergoods.orderId=`order`.orderid\n" +
                    "WHERE ordergoods.goodsId=? and `order`.userid=?";
            Object[] param={goodsId,userid};
            rs=this.executeQuery(sql,param);
            while (rs.next()){
                count=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<Order> orderFind(int userid) {
        List<Order> orders=new ArrayList<>();
        ResultSet rs=null;
        try {
            String sql="SELECT ordergoods.id,`order`.orderNumberid,`order`.purchaseTime,diamondmessage.goodspath,`order`.state,diamondmessage.diamondprice,ordergoods.goodsName\n" +
                    "FROM `order`\n" +
                    "INNER JOIN ordergoods ON ordergoods.orderId=`order`.orderid\n" +
                    "INNER JOIN diamondmessage ON diamondmessage.id=ordergoods.goodsId\n" +
                    "WHERE `order`.userid=?";
            Object[] parma={userid};
            rs=this.executeQuery(sql,parma);
            while (rs.next()){
                Order order=new Order();
                OrderGoods orderGoods=new OrderGoods();
                DiamondRingEntity diamondRingEntity=new DiamondRingEntity();
                orderGoods.setId(rs.getInt(1));
                order.setOrderNumber(rs.getString(2));
                order.setPurchaseTime(rs.getString(3));
                diamondRingEntity.setGoodsPath(rs.getString(4));
                order.setState(rs.getInt(5));
                diamondRingEntity.setDiamondPrice(rs.getInt(6));
                orderGoods.setGoodsName(rs.getString(7));
                order.setOrderGoods(orderGoods);
                order.setDiamondRingEntity(diamondRingEntity);
                orders.add(order);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return orders;
    }

    @Override
    public List<Order> orderInformation(String orderNumber) {
        List<Order> orders=new ArrayList<>();
        ResultSet rs=null;
        try {
            String sql="SELECT `order`.orderid,`order`.orderNumberid,`order`.state,`order`.purchaseTime,`order`.transactionPrice,\n" +
                    "`order`.remarks,useraddress.addName,useraddress.diamondProvince,useraddress.diamondCity,\n" +
                    "useraddress.diamondDistrict,useraddress.diamondStreet,useraddress.addPhone\n" +
                    "FROM `order`\n" +
                    "INNER JOIN useraddress ON useraddress.addressid=`order`.addressid\n" +
                    "WHERE `order`.orderNumberid=?";
            Object[] param={orderNumber};
            rs=this.executeQuery(sql,param);
            while (rs.next()){
                Order order=new Order();
                Address address=new Address();
                order.setOrderId(rs.getInt(1));
                order.setOrderNumber(rs.getString(2));
                order.setState(rs.getInt(3));
                order.setPurchaseTime(rs.getString(4));
                order.setTransactionPrice(rs.getInt(5));
                order.setRemarks(rs.getString(6));
                address.setAddName(rs.getString(7));
                address.setDiamondProvince(rs.getString(8));
                address.setDiamondCity(rs.getString(9));
                address.setDiamondDistrict(rs.getString(10));
                address.setDiamondStreet(rs.getString(11));
                address.setAddPhone(rs.getString(12));
                order.setAddress(address);
                orders.add(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public int orderCancel(int id) {
        int count=0;
        try {
            String sql="DELETE from ordergoods WHERE ordergoods.id=?";
            Object[] param={id};
            count=this.executeUpdate(sql,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}
